global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"

forvalues year = 1994/2018 {
  *local year  1994
  noisily di "Processing SSA DAF18 PUF annual file for `year'"
  
  local y = substr("`year'", 3, 4)

  * Load SSA DAFPUF annual file
  local keepvars pufpin DUED_PUF`y'?? 
  use `keepvars' using "$SSDIMed/data/raw/ssa/DAF18/daf18_ann`y'puf_stata.dta", clear

  * year of SSA DAFPUF annual file


  * rename variables
  rename DUED_PUF`y'?? dued_puf??
  * reshape long: individual-monthly observations
  greshape long dued_puf, by(pufpin) keys(month) string
  gcollapse (sum) dued_puf, by(pufpin) fast 

  * QC
  gisid pufpin 
  gen year = `year'
  * Sort and save
  sort pufpin year  
  compress _all 
  cap mkdir "$SSDIMed/data/proc/ssa"
  cap mkdir "$SSDIMed/data/proc/ssa/DAF18"
  save "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann`year'_long_cashbenefits.dta", replace
}

* make annual inflation factor 
use "$SSDIMed/data/proc/bls/cpi/bls_cpi_us_monthly.dta", clear
keep cpi_all_us_yyyy year 
duplicates drop _all, force 
save "$SSDIMed/data/proc/bls/cpi/bls_cpi_us_yearly.dta", replace


* append annual files
clear
forvalues year = 1994/2018 {
  append using "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann`year'_long_cashbenefits.dta"
}
merge m:1 pufpin using  "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann_dmg_sample.dta", keep(3) nogen 
gen ssdi_filing_month = mofd(bdof_puf1)
format ssdi_filing_month %tm
rename ssdi_filing_month yearmonth 
merge m:1 yearmonth using "$SSDIMed/data/raw/nationalunemployment/unemp_rate_national.dta", keep(3) nogen 
gen covstart_year=year(dofm(covstart_month))
gen years_since_covstart=year-covstart_year
merge m:1 year using "$SSDIMed/data/proc/bls/cpi/bls_cpi_us_yearly.dta"
rename dued_puf dued_pufnominal 
gen dued_puf=dued_pufnominal/cpi_all_us_yyyy

keep if inrange(age_year_covstart_ssdi, 21, 62) 
egen URile=xtile(unemp_rate_national), nq(20)
preserve 
collapse (mean) unemp_rate_national, by(URile) fast 
save "$SSDIMed/data/temp/nationalventiles.dta", replace 
restore 
reghdfe dued_puf i.URile, abs(years_since_covstart) cluster(covstart_month)
regsave using  "$SSDIMed/data/temp/cashbenefits_unemployment.dta", replace 
reghdfe dued_puf i.age_year_covstart_ssdi, abs(years_since_covstart) cluster(covstart_month)
regsave using  "$SSDIMed/data/temp/cashbenefits_ageatentry.dta", replace 
reghdfe dued_puf i.covstart_year, abs(years_since_covstart) cluster(covstart_month)
regsave using  "$SSDIMed/data/temp/cashbenefits_entryyear.dta", replace 

 
use "$SSDIMed/data/temp/cashbenefits_unemployment.dta", clear 
sum coef if var=="_cons" 
local cons=`r(mean)'
sum stderr if var=="_cons" 
local consse=`r(mean)'
drop if var=="_cons"
replace coef=coef+`cons'
replace stderr=sqrt(stderr^2+`consse'^2)
gen URile=_n 
gen LB=coef-1.96*stderr 
gen UB=coef+1.96*stderr 
merge 1:1 URile using "$SSDIMed/data/temp/nationalventiles.dta"
twoway (rarea UB LB unemp_rate_national, fcolor(gs13) lcolor(gs13)) (line coef unemp_rate_national, lwidth(thick) lcol(black)),  legend(off) graphregion(color(white))  bgcolor(white) ytitle("Annual Cash Benefits") xtitle("National unemployment at application") 
graph export $SSDIMed/results/figures/unemp_ventiles_cashbenefits.pdf, as(pdf) replace 


use "$SSDIMed/data/temp/cashbenefits_ageatentry.dta", clear 
sum coef if var=="_cons" 
local cons=`r(mean)'
sum stderr if var=="_cons" 
local consse=`r(mean)'
drop if var=="_cons"
replace coef=coef+`cons'
replace stderr=sqrt(stderr^2+`consse'^2)
gen age=_n+20 
gen LB=coef-1.96*stderr 
gen UB=coef+1.96*stderr 
twoway (rarea UB LB age, fcolor(gs13) lcolor(gs13)) (line coef age, lwidth(thick) lcol(black)),  legend(off) graphregion(color(white))  bgcolor(white) ytitle("Annual Cash Benefits") xtitle("Age at Entry") xline(50, lcol(black) lpat(dash)) xline(55,lcol(black) lpat(dash)) xlabel(20(5)60) 
graph export $SSDIMed/results/figures/ageatentry_cashbenefits.pdf, as(pdf) replace 



use "$SSDIMed/data/temp/cashbenefits_entryyear.dta", clear 
sum coef if var=="_cons" 
local cons=`r(mean)'
sum stderr if var=="_cons" 
local consse=`r(mean)'
drop if var=="_cons"
replace coef=coef+`cons'
replace stderr=sqrt(stderr^2+`consse'^2)
gen covstart_year=_n + 1992
merge 1:1 covstart_year using "$SSDIMed/data/proc/public/unempatapp_covstartyear.dta", keep(1 3)  nogen 
replace covstart_year=covstart_year-2
gen LB=coef-1.96*stderr 
gen UB=coef+1.96*stderr 
local UR "(line nat_unemp_mean covstart_year, yaxis(2) lpat(dash) lwid(thick) color("126 153 180"))"
twoway (rarea UB LB covstart_year, fcolor(gs13) lcolor(gs13)) `UR' (line coef covstart_year, lwidth(thick) lcol(black)),  legend(off) graphregion(color(white))  bgcolor(white) ytitle("Annual Cash Benefits (BLACK)", axis(1)) xtitle("Entry Year") xlabel(1990(4)2014) ylabel(4(2)10, axis(2)) ytitle("Nat'l Unemp Rate at App (BLUE)", axis(2)) 
graph export $SSDIMed/results/figures/entryyear_cashbenefits.pdf, as(pdf) replace 

